package com.gzhryc.shared_device.oem.code.profit.service;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.profit.dao.ProjectWalletOperateDao;
import com.gzhryc.shared_device.oem.code.profit.dao.db.ProjectWalletOperate;
import com.gzhryc.shared_device.oem.code.profit.service.dto.ProjectWalletOperateSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class ProjectWalletOperateService {

	static Logger log = Logger.getLogger(ProjectWalletOperateService.class);
	
	public static ProjectWalletOperateService self() {
		return MultiDBTools.getService(ProjectWalletOperateService.class, true);
	}

	public static ProjectWalletOperateService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey,ProjectWalletOperateService.class, true);
	}

	ProjectWalletOperateDao dao;
	
	public ProjectWalletOperateService(String jdbcKey){
		this.dao = new ProjectWalletOperateDao(jdbcKey);
	}

	public List<ProjectWalletOperate> findBySearch(ProjectWalletOperateSearch search, Integer pageNo, Integer pageLimit){
		Conditions condition = dao.buildConditions(search);
		condition.desc("operateDate");
		try {
			return dao.findByCondition(condition,pageNo,pageLimit);
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return null;
	}

	public Long countBySearch(ProjectWalletOperateSearch search){
		Conditions condition = dao.buildConditions(search);
		try {
			return dao.count(condition);
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return 0L;
	}

	public boolean addOperateRecord(Integer walletId,Long accountGroupId,Integer type,Long money,String operateNote){
		ProjectWalletOperate operate = new ProjectWalletOperate();
		operate.setWalletId(walletId);
		operate.setAccountGroupId(accountGroupId);
		operate.setType(type);
		operate.setMoney(money);
		operate.setOperateNote(operateNote);
		operate.setOperateDate(new Date());
		try {
			return dao.insert(operate) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}
